package com.wxd.woff.table;

import com.wxd.woff.AbstractOTFDataStream;
import com.wxd.woff.AbstractWoffTable;
import com.wxd.woff.ByteBufferOTFDataStream;
import com.wxd.woff.WoffFont;

import java.io.IOException;
import java.util.Arrays;

/**
 * os/2 table
 * Created with IntelliJ IDEA.
 *
 * @author: wangxindong
 * @date: 2018/9/1-22:30
 */
public class OS2WindowsMetricsTable extends AbstractWoffTable {

    public static final String TAG = "OS/2";

    private int version;
    private short averageCharWidth;
    private int weightClass;
    private int widthClass;
    private short fsType;
    private short subscriptXSize;
    private short subscriptYSize;
    private short subscriptXOffset;
    private short subscriptYOffset;
    private short superscriptXSize;
    private short superscriptYSize;
    private short superscriptXOffset;
    private short superscriptYOffset;
    private short strikeoutSize;
    private short strikeoutPosition;
    private int familyClass;
    private byte[] panose = new byte[10];
    private long unicodeRange1;
    private long unicodeRange2;
    private long unicodeRange3;
    private long unicodeRange4;
    private String achVendId = "XXXX";
    private int fsSelection;
    private int firstCharIndex;
    private int lastCharIndex;
    private int typoAscender;
    private int typoDescender;
    private int typoLineGap;
    private int winAscent;
    private int winDescent;
    private long codePageRange1 = -1;
    private long codePageRange2 = -1;
    private int sxHeight;
    private int sCapHeight;
    private int usDefaultChar;
    private int usBreakChar;
    private int usMaxContext;


    public OS2WindowsMetricsTable(WoffFont woffFont) {
        super(woffFont);
    }

    @Override
    public void read(WoffFont woffFont, AbstractOTFDataStream stream) throws IOException {
        stream.seek(offset);
        byte[] fontData = stream.read(compLength);
        byte[] inflateFont = inflateFontData(fontData);
        AbstractOTFDataStream data = new ByteBufferOTFDataStream(inflateFont);

        version = data.readUnsignedShort();
        averageCharWidth = data.readSignedShort();
        weightClass = data.readUnsignedShort();
        widthClass = data.readUnsignedShort();
        fsType = data.readSignedShort();
        subscriptXSize = data.readSignedShort();
        subscriptYSize = data.readSignedShort();
        subscriptXOffset = data.readSignedShort();
        subscriptYOffset = data.readSignedShort();
        superscriptXSize = data.readSignedShort();
        superscriptYSize = data.readSignedShort();
        superscriptXOffset = data.readSignedShort();
        superscriptYOffset = data.readSignedShort();
        strikeoutSize = data.readSignedShort();
        strikeoutPosition = data.readSignedShort();
        familyClass = data.readSignedShort();
        panose = data.read(10);
        unicodeRange1 = data.readUnsignedInt();
        unicodeRange2 = data.readUnsignedInt();
        unicodeRange3 = data.readUnsignedInt();
        unicodeRange4 = data.readUnsignedInt();
        achVendId = data.readString(4);
        fsSelection = data.readUnsignedShort();
        firstCharIndex = data.readUnsignedShort();
        lastCharIndex = data.readUnsignedShort();
        typoAscender = data.readSignedShort();
        typoDescender = data.readSignedShort();
        typoLineGap = data.readSignedShort();
        winAscent = data.readUnsignedShort();
        winDescent = data.readUnsignedShort();
        if (version >= 1) {
            codePageRange1 = data.readUnsignedInt();
            codePageRange2 = data.readUnsignedInt();
        }
        if (version >= 1.2) {
            sxHeight = data.readSignedShort();
            sCapHeight = data.readSignedShort();
            usDefaultChar = data.readUnsignedShort();
            usBreakChar = data.readUnsignedShort();
            usMaxContext = data.readUnsignedShort();
        }
    }

    @Override
    public String toString() {
        return "AbstractWoffTable{" +
                "tag='" + tag + '\'' +
                ", offset=" + offset +
                ", compLength=" + compLength +
                ", origLength=" + origLength +
                ", origChecksum=" + origChecksum +
                ", version=" + version +
                ", averageCharWidth=" + averageCharWidth +
                ", weightClass=" + weightClass +
                ", widthClass=" + widthClass +
                ", fsType=" + fsType +
                ", subscriptXSize=" + subscriptXSize +
                ", subscriptYSize=" + subscriptYSize +
                ", subscriptXOffset=" + subscriptXOffset +
                ", subscriptYOffset=" + subscriptYOffset +
                ", superscriptXSize=" + superscriptXSize +
                ", superscriptYSize=" + superscriptYSize +
                ", superscriptXOffset=" + superscriptXOffset +
                ", superscriptYOffset=" + superscriptYOffset +
                ", strikeoutSize=" + strikeoutSize +
                ", strikeoutPosition=" + strikeoutPosition +
                ", familyClass=" + familyClass +
                ", panose=" + Arrays.toString(panose) +
                ", unicodeRange1=" + unicodeRange1 +
                ", unicodeRange2=" + unicodeRange2 +
                ", unicodeRange3=" + unicodeRange3 +
                ", unicodeRange4=" + unicodeRange4 +
                ", achVendId='" + achVendId + '\'' +
                ", fsSelection=" + fsSelection +
                ", firstCharIndex=" + firstCharIndex +
                ", lastCharIndex=" + lastCharIndex +
                ", typoAscender=" + typoAscender +
                ", typoDescender=" + typoDescender +
                ", typoLineGap=" + typoLineGap +
                ", winAscent=" + winAscent +
                ", winDescent=" + winDescent +
                ", codePageRange1=" + codePageRange1 +
                ", codePageRange2=" + codePageRange2 +
                ", sxHeight=" + sxHeight +
                ", sCapHeight=" + sCapHeight +
                ", usDefaultChar=" + usDefaultChar +
                ", usBreakChar=" + usBreakChar +
                ", usMaxContext=" + usMaxContext +
                '}';
    }
}
